<?php

/**
 * This is the model class for table "{{facility}}".
 *
 * The followings are the available columns in table '{{facility}}':
 * @property integer $id
 * @property string $title
 * @property string $description
 * @property string $address
 * @property integer $create_time
 * @property integer $update_time
 * @property integer $teacher_id
 * @property integer $status
 *
 * The followings are the available model relations:
 * @property Class[] $classes
 * @property Teacher $teacher
 */
class Facility extends CActiveRecord
{

	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Facility the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{facility}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('title', 'required'),
			array('create_time, update_time, teacher_id, status', 'numerical', 'integerOnly'=>true),
			array('title, address', 'length', 'max'=>255),
			array('description', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, title, description, address, create_time, update_time, teacher_id, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'classes' => array(self::HAS_MANY, 'GuitarClass', 'facility_id'),
			'classCount' => array(self::STAT, 'GuitarClass', 'facility_id'),
			'teacher' => array(self::BELONGS_TO, 'Teacher', 'teacher_id'),
            'teachers' => array(self::MANY_MANY, 'Teacher', 'tbl_facility_teacher(facility_id,teacher_id)'),
            'teacherCount' => array(self::STAT, 'FacilityTeacher', 'facility_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'title' => 'Tiêu đề',
			'description' => 'Miêu tả',
			'address' => 'Địa chỉ',
			'create_time' => 'Thời gian tạo',
			'update_time' => 'Thời gian cập nhật',
			'teacher_id' => 'Giáo viên CN',
			'status' => 'Trạng thái',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('create_time',$this->create_time);
		$criteria->compare('update_time',$this->update_time);
		$criteria->compare('teacher_id',$this->teacher_id);
		$criteria->compare('status',$this->status);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

    public function beforeSave()
    {

        if ($this->isNewRecord) {
            $this->create_time = time();
        } else {
            $this->update_time = time();
        }
        return parent::beforeSave();
    }

    public static function listTeacher()
    {
        $teachers = Teacher::model()->findAll();
        $items = array();
        if ($teachers) {
            foreach ($teachers as $teacher) {
                $items[$teacher['id']] = $teacher['name'];
            }
        }

        return $items;
    }
}